package com.service.setting.impl;

import com.comm.R;
import com.mapper.setting.SysEmployeeMapper;
import com.service.setting.SysEmployeeService;
import com.utils.JwtUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.UUID;


@Service
public class SysEmployeeServiceImpl implements SysEmployeeService {
    @Autowired
    private SysEmployeeMapper employeeMapper;



    @Override
    public R login(String userName, String password, HttpServletResponse response) {

        R resultData=R.defaulted();
        try{


            Subject subject = SecurityUtils.getSubject();
            UsernamePasswordToken usernamePasswordToken=new UsernamePasswordToken(userName,password);
            subject.login(usernamePasswordToken);
            resultData.setCode(200).setMsg("登录成功！");
            String currentToken = JwtUtils.createJwt(UUID.randomUUID().toString(), JwtUtils.JWT_SECERT, userName, JwtUtils.TTMILLIS);
            String refreshToken=JwtUtils.createJwt(UUID.randomUUID().toString(),JwtUtils.JWT_SECERT,userName,JwtUtils.TTMILLIS*2);

            response.addHeader("current_Authorization",currentToken);
            response.addHeader("refresh_Authorization",refreshToken);



        }catch (UnknownAccountException e) {

        } catch (IncorrectCredentialsException e){
            resultData.setMsg("用户密码错误");
            System.out.println("用户密码错误");
        } catch (UnauthorizedException e){
            resultData.setMsg("角色错误");

            System.out.println("角色错误");
        } catch (LockedAccountException e){
            resultData.setMsg("账号被锁");

            System.out.println("账号被锁");
        } catch (AuthenticationException e){
            resultData.setMsg("认证错误");
            System.out.println("认证错误");
        }


        return resultData;
    }
}
